package com.kerrykidz.system.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.system.service.IIndexService;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.util.security.PrincipalInfo;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.system.vo.WarnVo;
import com.kerrykidz.workflow.dao.model.ActivitiEntity;
import com.kerrykidz.workflow.vo.SearchParamVo;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: IndexController
 * @description: 首页显示
 * @author: zhuss
 * @date: 2014年9月25日 下午6:52:00
 * @version: V1.0
 * 
 */
@Controller
@RequestMapping(value = "/system/index")
public class IndexController {
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private IIndexService indexService;

	@RequestMapping(value = "urlauth/{id}")
	public String urlauth(@PathVariable("id") String id) {
		
		//判断当前是首页
		if("A1".equals(id.toUpperCase())){
			return "redirect:/system/index.shtml";
		}
		
		PrincipalInfo principal = PrincipalInfo.getInstance();
		List<String> links = principal.getUrlsLinks().get(id);
		if (links != null && links.size() > 0) {
			return "redirect:/" + links.get(0);
		}
		return "noAuthority";
	}

	/**
	 * 查询首页显示
	 * 
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping()
	@ResponseBody
	public ModelAndView index(HttpSession session, @RequestParam(value = "mode", required = false) String mode)
			throws RDPException {
		String jspPath = "index";
		if (null != mode && "pad".equals(mode)) {
			jspPath = "pad/index";
		}
		ModelAndView mv = new ModelAndView(jspPath);
		UserInfo user = UserUtil.getUserFromSession();
		if (null != user) {
			mv.addObject("index", indexService.queryIndex(user));
			mv.addObject("user", user);
		}
		return mv;
	}

	/**
	 * 跳转
	 * 
	 * @param key
	 * @return
	 * @date: 2014年9月28日
	 */
	@RequestMapping(value = "/findNoConfirm", method = RequestMethod.GET)
	public String findNoConfirm() {
		return "indexPage/index_noConfirm_list";
	}

	/**
	 * 待确认的派对团体（5日内将要举办的派对团体）
	 * 
	 * @param searchParam
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping(value = "/findNoConfirm", method = RequestMethod.POST)
	@ResponseBody
	public SearchParamVo findNoConfire(SearchParamVo searchParam,
			HttpSession session) throws RDPException {
		searchParam.setIspaging(true);
		if (null != UserUtil.getUserFromSession()) {
			searchParam.setUserInfo(UserUtil.getUserFromSession());
			searchParam.setResults(indexService.queryNoConfirm(searchParam));
		}
		return searchParam;
	}

	/**
	 * 跳转
	 * 
	 * @param key
	 * @return
	 * @date: 2014年9月28日
	 */
	@RequestMapping(value = "/findNoPay", method = RequestMethod.GET)
	public String findNoPay() {
		return "indexPage/index_noPay_list";
	}

	/**
	 * 预约后24小时内未预付的团体
	 * 
	 * @param searchParam
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping(value = "/findNoPay", method = RequestMethod.POST)
	@ResponseBody
	public SearchParamVo findNoPay(SearchParamVo searchParam,
			HttpSession session) throws RDPException {
		searchParam.setIspaging(true);
		if (null != UserUtil.getUserFromSession()) {
			searchParam.setUserInfo(UserUtil.getUserFromSession());
			searchParam.setResults(indexService.queryNoPay(searchParam));
		}
		return searchParam;
	}

	/**
	 * 跳转
	 * 
	 * @param key
	 * @return
	 * @date: 2014年9月28日
	 */
	@RequestMapping(value = "/findEndMem", method = RequestMethod.GET)
	public String findEndMem() {
		return "indexPage/index_endMem_list";
	}

	/**
	 * 即将到期会员
	 * 
	 * @param searchParam
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping(value = "/findEndMem", method = RequestMethod.POST)
	@ResponseBody
	public SearchParamVo findEndMem(SearchParamVo searchParam,
			HttpSession session) throws RDPException {
		searchParam.setIspaging(true);
		if (null != UserUtil.getUserFromSession()) {
			searchParam.setUserInfo(UserUtil.getUserFromSession());
			searchParam.setResults(indexService.queryEndMem(searchParam));
		}
		return searchParam;
	}

	/**
	 * 跳转
	 * 
	 * @param key
	 * @return
	 * @date: 2014年9月28日
	 */
	@RequestMapping(value = "/findBirthdayMem", method = RequestMethod.GET)
	public String findBirthdayMem() {
		return "indexPage/index_birthdayMem_list";
	}

	/**
	 * 近期生日会员
	 * 
	 * @param searchParam
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping(value = "/findBirthdayMem", method = RequestMethod.POST)
	@ResponseBody
	public SearchParamVo findBirthdayMem(SearchParamVo searchParam,
			HttpSession session) throws RDPException {
		searchParam.setIspaging(true);
		if (null != UserUtil.getUserFromSession()) {
			searchParam.setUserInfo(UserUtil.getUserFromSession());
			searchParam.setResults(indexService.queryBirthdayMem(searchParam));
		}
		return searchParam;
	}

	/**
	 * 跳转
	 * 
	 * @param key
	 * @return
	 * @date: 2014年9月28日
	 */
	@RequestMapping(value = "/findScmCount", method = RequestMethod.GET)
	public String findScmCount() {
		return "indexPage/index_scmCount_list";
	}

	/**
	 * 库存数量
	 * 
	 * @param searchParam
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping(value = "/findScmCount", method = RequestMethod.POST)
	@ResponseBody
	public SearchParamVo findScmCount(SearchParamVo searchParam,
			HttpSession session) throws RDPException {
		searchParam.setIspaging(true);
		if (null != UserUtil.getUserFromSession()) {
			searchParam.setUserInfo(UserUtil.getUserFromSession());
			searchParam.setResults(indexService.queryScmCount(searchParam));
		}
		return searchParam;
	}

	/**
	 * 查询场内人数
	 * 
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月26日
	 */
	@RequestMapping("/floorNum")
	@ResponseBody
	public WarnVo floorNum() throws RDPException {
		return indexService.indexFloorNum();
	}

	/**
	 * 查询预警库存数量
	 * 
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月26日
	 */
	@RequestMapping("/scmCount")
	@ResponseBody
	public WarnVo scmCount(HttpSession session) throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		SearchParamVo vo = new SearchParamVo();
		if (null != user) {
			if (StringUtils.isNotBlank(user.getBranId())) {
				vo.setUserInfo(user);
				vo.setType("1");
				return indexService.indexWarnScm(vo);
			}
		}
		return null;
	}

	/**
	 * 跳转
	 * 
	 * @param key
	 * @return
	 * @date: 2014年9月28日
	 */
	@RequestMapping(value = "/findTodoTask", method = RequestMethod.GET)
	public String findTodoTask() {
		return "indexPage/workflow_todo_list";
	}

	/**
	 * 查询待办列表
	 * 
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "/findTodoTask", method = RequestMethod.POST)
	@ResponseBody
	public SearchParamVo findTodoTask(SearchParamVo searchParam, HttpSession session) throws RDPException {
		logger.debug("SearchParamVo request: ", searchParam);
		searchParam.setIspaging(true);
		if (null != UserUtil.getUserFromSession()) {
			searchParam.setUserInfo(UserUtil.getUserFromSession());
			List<ActivitiEntity> list = indexService.queryTodoTask(searchParam);
			searchParam.setResults(list);
		}
		return searchParam;
	}

	/**
	 * 跳转
	 * 
	 * @param key
	 * @return
	 * @date: 2014年9月28日
	 */
	@RequestMapping(value = "/findFinishTask", method = RequestMethod.GET)
	public String findFinishTask() {
		return "indexPage/workflow_finish_list";
	}

	/**
	 * 查询已办列表
	 * 
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "/findFinishTask", method = RequestMethod.POST)
	@ResponseBody
	public SearchParamVo findFinishTask(SearchParamVo searchParam, HttpSession session) throws RDPException {
		logger.debug("SearchParamVo request: ", searchParam);
		searchParam.setIspaging(true);
		if (null != UserUtil.getUserFromSession()) {
			searchParam.setUserInfo(UserUtil.getUserFromSession());
			searchParam.setResults(indexService.queryFinishTask(searchParam));
		}
		return searchParam;
	}

	/**
	 * 
	 * @param businessKey
	 *            :业务ID
	 * @param key
	 *            :流程key
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月29日
	 */
	@RequestMapping("/jumpWorkflow")
	public String jumpWorkflow(String businessKey, String key,
			HttpSession session) throws RDPException {
		if (StringUtils.isNotBlank(businessKey) && StringUtils.isNotBlank(key)) {
			if (key.equals("leave")) {// 请假流程
				return "redirect:/workflow/leave/detailLeave.shtml?id="
						+ businessKey + "";
			} else if (key.equals("stock")) {// 库存调整
				return "redirect:/StockCorrect/toStockShenheCorrect.shtml?pkStgadjId="
						+ businessKey + "&url=stock/stock_shenheCorrect";
			} else if (key.equals("discount")) {// 限时打折
				return "redirect:/promotionWorkFlow/seacheByCampIdWfHis.shtml?pkCampId="
						+ businessKey + "";
			} else if (key.equals("price")) {// 定价
				return "redirect:/scmInvocing/priceManager/handPriceForBranchPage.shtml?pkPriceId="
						+ businessKey + "";
			} else if (key.equals("server")) {// 客服申请
				return "redirect:/filemanagement/customer/handleServicePage.shtml?pkCareformId="
						+ businessKey + "";
			} else if (key.equals("pay")) {// 付款申请
				return "redirect:/scmInvocing/scmPayForm/invoOrderPayNew.shtml?type=chuli&pkPayFormId="
						+ businessKey + "";
			} else if (key.equals("order")) {// 采购订单
				return "redirect:/scmInvocing/scmPurOrder/invoOrderNew.shtml?type=chuli&pkPurordId="
						+ businessKey + "";
			}
		}
		return null;
	}

}
